// SPDX-License-Identifier: Mulan PSL v2
/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * This software is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

//! 临时的protobuf类型定义，用于编译测试

use serde::{Deserialize, Serialize};

/// 请求记录文件头部
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RecordFileHeader {
    pub magic: u32,
    pub version: String,
    pub created_timestamp_ns: u64,
    pub system_context: Option<SystemContext>,
    pub statistics: Option<RecordStatistics>,
}

/// 系统上下文信息
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemContext {
    pub hostname: String,
    pub process_id: u32,
    pub process_name: String,
    pub cuda_version: String,
}

/// 记录统计信息
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct RecordStatistics {
    pub total_records: u64,
    pub total_size_bytes: u64,
    pub first_timestamp_ns: u64,
    pub last_timestamp_ns: u64,
}

/// 单个请求记录
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RequestRecord {
    pub record_id: u64,
    pub timestamp_ns: u64,
    pub thread_id: u64,
    pub method_id: u64,
    pub arguments: Vec<ArgumentRecord>,
    pub raw_data: Vec<u8>,
    pub data_checksum: u32,
}

/// 参数记录
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ArgumentRecord {
    pub arg_index: u32,
    pub arg_type: i32,
    pub size_bytes: u64,
    pub alignment: u32,
    pub flags: Option<ArgumentFlags>,
    pub data_offset: u64,
}

/// 参数类型枚举
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ArgumentType {
    ArgTypeUnknown = 0,
    ArgTypeValue = 1,
    ArgTypePointer = 2,
    ArgTypeBuffer = 3,
    ArgTypeHandle = 4,
}

impl TryFrom<i32> for ArgumentType {
    type Error = ();
    
    fn try_from(value: i32) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(ArgumentType::ArgTypeUnknown),
            1 => Ok(ArgumentType::ArgTypeValue),
            2 => Ok(ArgumentType::ArgTypePointer),
            3 => Ok(ArgumentType::ArgTypeBuffer),
            4 => Ok(ArgumentType::ArgTypeHandle),
            _ => Err(()),
        }
    }
}

/// 参数标志
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ArgumentFlags {
    pub is_input: bool,
    pub is_output: bool,
    pub is_const: bool,
    pub is_optional: bool,
}

/// 记录批次
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RecordBatch {
    pub records: Vec<RequestRecord>,
    pub batch_timestamp_ns: u64,
    pub batch_id: u32,
}

/// 文件尾部
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RecordFileFooter {
    pub indices: Vec<RecordIndex>,
    pub header_checksum: u32,
    pub data_checksum: u32,
}

/// 记录索引项
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RecordIndex {
    pub record_id: u64,
    pub file_offset: u64,
    pub record_size: u32,
    pub timestamp_ns: u64,
}